package com.zofer.study.stream;

import com.zofer.study.stream.entity.Dish;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 用流收集数据
 */
public class StreamTest03 {

    static List<Dish> menu = Arrays.asList(
            new Dish("pork", false, 800, Dish.Type.MEAT),
            new Dish("beef", false, 700, Dish.Type.MEAT),
            new Dish("chicken", false, 400, Dish.Type.MEAT),
            new Dish("french fries", true, 530, Dish.Type.OTHER),
            new Dish("rice", true, 350, Dish.Type.OTHER),
            new Dish("season fruit", true, 120, Dish.Type.OTHER),
            new Dish("pizza", true, 550, Dish.Type.OTHER),
            new Dish("salmon", false, 300, Dish.Type.FISH),
            new Dish("salmon", false, 450, Dish.Type.FISH) );

    public static void main(String[] args) {
        test2();
        test3();
        test4();
    }


    /**
     * Collectors.maxBy和 Collectors.minBy
     */
    public static void test2(){
        Comparator<Dish> cp = Comparator.comparingInt(Dish::getCalories);
        Optional<Dish> dish = menu.stream().collect(Collectors.maxBy(cp));
        System.out.println("========:"+dish.get());

        int max = menu.stream().map(Dish::getCalories).max(Integer::max).get();
        System.out.println(max);
    }


    /**
     * averagingInt  summarizingInt
     * IntSummaryStatistics{count=9, sum=4300, min=120,average=477.777778, max=800}
     */
    public static void test3(){
        IntSummaryStatistics intSummaryStatistics = menu.stream().collect(Collectors.summarizingInt(Dish::getCalories));
        System.out.println("==========="+intSummaryStatistics);

        Double a = menu.stream().collect(Collectors.averagingInt(Dish::getCalories));
        System.out.println("===========a:"+a);

    }


    /**
     * 字符串拼接
     * join()
     */
    public static void test4(){
        String str1 = menu.stream().map(Dish::getName).collect(Collectors.joining());
        String str2 = menu.stream().map(Dish::getName).collect(Collectors.joining(","));
        String str3 = menu.stream().map(Dish::getName).collect(Collectors.joining(",","@","*"));
        System.out.println(str1);
        System.out.println(str2);
        System.out.println(str3);
    }







}
